package com.me.mygdxgame.renderer.RenderObjects;

import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.loaders.wavefront.ObjLoader;

public class MeshManager
{

	private static MeshManager instance = null;
	
	ObjLoader loader = new ObjLoader();
	
	private MeshManager()
	{
		
	}
	
	public static MeshManager getInstance()
	{
		if(instance == null)
		{
			instance = new MeshManager();
		}
		
		return instance;
	}
	
	private final static String ATTRIBUTE_POSITION = "a_Position";
//	private final static String ATTRIBUTE_NORMAL = "a_Normal";
	private final static String ATTRIBUTE_TEXTURE = "a_texCoords";
	
	public Mesh genCube()
	{

		Mesh mesh = new Mesh(true, 24, 36, new VertexAttribute(Usage.Position,
				3, ATTRIBUTE_POSITION), new VertexAttribute(
				Usage.TextureCoordinates, 2, ATTRIBUTE_TEXTURE));

		final float[] textureCoordinateData = {

		0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
				0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
				1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
				0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
				0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, };

		// X, Y, Z
		final float[] cubeVerts = {
				// In OpenGL counter-clockwise winding is default. This means
				// that when we look at a triangle,
				// if the points are counter-clockwise we are looking at the
				// "front". If not we are looking at
				// the back. OpenGL has an optimization where all back-facing
				// triangles are culled, since they
				// usually represent the backside of an object and aren't
				// visible anyways.

				-0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f,
				0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f,
				0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f,
				-0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f,
				-0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f,
				-0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f,
				0.5f, 0.5f, -0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f,
				-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, };

//		final float[] cubeNormalData = { 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
//				0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
//				1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
//				-1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,
//				0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
//				0.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
//				0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
//				1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, };

		float[] vertices = new float[24 * 5];
		int pIdx = 0;
//		int nIdx = 0;
		int tIdx = 0;
		for (int i = 0; i < vertices.length;)
		{
			vertices[i++] = cubeVerts[pIdx++];
			vertices[i++] = cubeVerts[pIdx++];
			vertices[i++] = cubeVerts[pIdx++];
//			vertices[i++] = cubeNormalData[nIdx++];
//			vertices[i++] = cubeNormalData[nIdx++];
//			vertices[i++] = cubeNormalData[nIdx++];
			vertices[i++] = textureCoordinateData[tIdx++];
			vertices[i++] = textureCoordinateData[tIdx++];
		}

		short[] indices = { 0, 2, 1, 0, 3, 2, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8,
				10, 11, 12, 15, 14, 12, 14, 13, 16, 17, 18, 16, 18, 19, 20, 23,
				22, 20, 22, 21 };
		
		mesh.setVertices(vertices);
		mesh.setIndices(indices);
		
		return mesh;
	}

	
	
}
